package ui;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import logic.Visualization;

import data.Node;

public class NodeAttributeDialog extends Dialog {
	JTextField name;
	JSlider size;
	JComboBox image;
	String [] imagesStrings;
	Node node;
	String tempName;
	String tempGraphic;
	float tempSize;
	/**
	 * a dialog to edit the name, size and choose the image of a node
	 * @param owner father window (dialog is modal)
	 * @param node node to edit
	 */
	public NodeAttributeDialog(Frame owner, Node node) {
		super(owner);
		this.setModal(true);
		this.node = node;
		tempName = node.getName();
		tempGraphic = node.getGraphic();
		tempSize = node.getSize();
		
		JPanel panel = new JPanel();
		this.setTitle("Edit Node with ID "+node.getID());
		this.add(panel);
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 5;
		c.weighty = 1;
		
		JLabel nameLabel = new JLabel("Name:");
		c.gridx = 0;
		c.gridy = 0;
		panel.add(nameLabel, c);
		
		name = new JTextField(node.getName());
		name.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				NodeAttributeDialog.this.node.setName(((JTextField)e.getSource()).getText());
				}
			public void keyPressed(KeyEvent e) {}
		});
		c.gridx = 1;
		c.gridy = 0;
		panel.add(name, c);

		JLabel sizeLabel = new JLabel("Size:");
		c.gridx = 0;
		c.gridy = 1;
		panel.add(sizeLabel, c);
		
		size = new JSlider(0,1000,(int)node.getSize()*100);
		size.addChangeListener(new ChangeListener() {			
			@Override
			public void stateChanged(ChangeEvent e) {
				NodeAttributeDialog.this.node.setSize(((JSlider)e.getSource()).getValue()/100.0f);
			}
		});
		c.gridx = 1;
		c.gridy = 1;
		panel.add(size, c);
		c.gridx++;

		JLabel imageLabel = new JLabel("Image:");
		c.gridx = 0;
		c.gridy = 2;
		panel.add(imageLabel, c);
		
		imagesStrings = (new File("data"+File.separator+"pics")).list();
		image = new JComboBox(imagesStrings);
		for(int i = 0; i < imagesStrings.length; i++){
			if (imagesStrings[i].equals(node.getGraphic())){
				image.setSelectedIndex(i);
				break;
			}
		}
		image.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String selection = (String)((JComboBox)arg0.getSource()).getSelectedItem();
				NodeAttributeDialog.this.node.setGraphic(selection);
			}
		});
		c.gridx = 1;
		c.gridy = 2;
		panel.add(image, c);
		c.gridx++;
		

		JButton ok = new JButton("OK");
		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				NodeAttributeDialog.this.dispose();
				//System.out.println(NodeAttributeDialog.this.node);
				if(NodeAttributeDialog.this.tempSize != NodeAttributeDialog.this.node.getSize())
					new Visualization().start();
			}
		});
		c.gridx = 1;
		c.gridy = 3;
		panel.add(ok, c);
		c.gridx++;

		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				NodeAttributeDialog.this.node.setName(NodeAttributeDialog.this.tempName);
				NodeAttributeDialog.this.node.setGraphic(NodeAttributeDialog.this.tempGraphic);
				NodeAttributeDialog.this.node.setSize(NodeAttributeDialog.this.tempSize);
				NodeAttributeDialog.this.dispose();
				//System.out.println(NodeAttributeDialog.this.node);
			}
		});
		c.gridx = 0;
		c.gridy = 3;
		panel.add(cancel, c);
		c.gridx++;
		this.pack();
		this.setVisible(true);
	}

	private static final long serialVersionUID = 1L;

}
